
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode invertTree(TreeNode root) {
        //边界检查
        if (root == null){
            return null;
        }
        //递归调用
        recursiveConvert(root);
        return root;
    }

    private void recursiveConvert(TreeNode root){
        TreeNode temp;
        if (root.right == null && root.left == null)    //如果是叶结点就return即可
            return;
        // 下面三行就是 swap(left,right);
        temp = root.left;
        root.left = root.right;
        root.right = temp;

        if (root.left != null)
            recursiveConvert(root.left);
        if (root.right != null)
            recursiveConvert(root.right);
    }
}
//leetcode submit region end(Prohibit modification and deletion)
